package com.itheima.controller;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.itheima.bean.Employee;
import com.itheima.bean.PageParam;
import com.itheima.bean.R;
import com.itheima.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;
import java.util.Map;

@RestController
public class EmployeeController {
    //注入对象
    @Autowired
    private EmployeeService es;

    //定义全局变量
    R r = null;

    /**
     * 登录功能
     * @param employee
     * @return
     */
    @PostMapping("/employee/login")
    //@PostMapping("/employees")
    public R<String> login(@RequestBody Employee employee, HttpSession session){
        //1、接收参数

        //2、调用service
        Employee loginEmployee = es.login(employee);

        //3、相应
        if (loginEmployee != null){//登录成功
            //设置响应数据
            r = R.success("登录成功");
            //把用户对象保存到session作用域
            session.setAttribute("employee",loginEmployee);
        } else {//登录失败
            r = R.error("用户名或者密码错误");
        }
        return r;
    }

    /**
     * 退出功能
     * @param session
     * @return
     */
    @PostMapping("/employee/logout")
    public R<String> logout(HttpSession session){

        //删除session里面的employee数据
        //session.removeAttribute("employee");

        //销毁session 狠一点
        session.invalidate();

        //通常很少出现退出失败的情况，如果真的想写失败的代码，抓个异常就好
        try {
            return R.success("退出成功");
        } catch (Exception e) {
            e.printStackTrace();
            return R.error("退出失败");
        }
    }

    /**
     * 添加功能
     * @param employee
     * @return
     */
    @PostMapping("/employee")
    public R<String> add(@RequestBody Employee employee  , HttpSession session){
        System.out.println("controller..add=====" + Thread.currentThread().getId());
        /*//1、填充数据
        employee.setCreateTime(LocalDateTime.now());
        employee.setUpdateTime(LocalDateTime.now());

        Employee e = (Employee)session.getAttribute("employee");
        //1.2 设置是谁添加进来这个用户, 是哪个用户更新的。
        employee.setCreateUser(e.getId());
        employee.setUpdateUser(e.getId());*/

        //1.3、设置密码的初始值，默认初始密码是:123456
        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));

        //2、调用service
        int row = es.add(employee);
        if (row > 0){
            return R.success("添加成功");
        } else {
            return R.error("添加失败");
        }
    }

    /**
     * 分页功能
     * @param param
     * @return
     */
    @GetMapping("/employee/{param}")
    public R<IPage> findPage(@PathVariable String param){
        System.out.println("param = " + param);
        String json = JSON.toJSONString(param);
        //1、第一种写法: 可以把这个json转化成一个map集合
        /*Map<String,Object> map = JSON.parseObject(param, Map.class);
        System.out.println("map = " + map);
        System.out.println("page = " + map.get("page"));
        System.out.println("pageSize = " + map.get("pageSize"));*/

        //2、第二种写法: 把json字符串转化成一个JavaBean对象
        PageParam pageParam = JSON.parseObject(param, PageParam.class);
        System.out.println("pageParam = " + pageParam);

        //3、调用service干活
        IPage<Employee> page = es.findPage(pageParam);

        return R.success(page);
    }

    /**
     * 修改功能
     * @param employee
     * @return
     */
    @PutMapping("/employee")
    public R<String> update(@RequestBody Employee employee , HttpSession session){
        //1、填充数据：修改员工需要设置修改的时间和是哪个用户修改的
        employee.setUpdateTime(LocalDateTime.now());
        Employee e = (Employee) session.getAttribute("employee");
        employee.setUpdateUser(e.getId());
        //2、调用service
        int row = es.update(employee);

        if (row > 0){
            return R.success("修改成功");
        } else {
            return R.error("修改失败");
        }
    }

































    /**
     * 登录功能 使用get请求方式的话
     * @param data
     * @return

    //@GetMapping("/employee/login2")
    //public R login2(@RequestBody Employee employee, HttpSession session){
    */
    @GetMapping("/employee/login2/{data}")
    public R<String> login2(@PathVariable String data, HttpSession session) {

        //这里收到的data 仅仅是前端页面传递过来的一个json字符串，需要手动转化成JavaBean对象。
        System.out.println("data = " + data);

        return null;
    }
}
